package org.jks.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.jks.config.Config;
import org.jks.constant.Constants;
import org.jks.model.User;
import org.jks.service.UserService;
import org.jks.util.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/infoQry")
public class InfoQryController {
	@RequestMapping("/")
	public ModelAndView index(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		User u = (User)session.getAttribute(Constants.SESSION_ACCOUNT);
		model.put("user", JsonUtil.Object2Json(u));
		return new ModelAndView("infoQry/index", model);
	}
	
	@RequestMapping("/index")
	public ModelAndView index1(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/index", model);
	}
	
	@RequestMapping("/yg_info")
	public ModelAndView yg_info(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/yg_info", model);
	}
	
	@RequestMapping("/zy_qry")
	public ModelAndView zy_qry(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/zy_qry", model);
	}
	
	@RequestMapping("/sb_qry")
	public ModelAndView sb_qry(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/sb_qry", model);
	}
	
	@RequestMapping("/zl_qry")
	public ModelAndView zl_qry(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/zl_qry", model);
	}
	
	@Autowired
	UserService userService;
	@RequestMapping("/yg_info/{id}")
	public ModelAndView detail(@PathVariable long id, HttpServletRequest request, HttpSession session, HttpServletResponse response, Map<String,Object> model){
		try {
			User user = userService.findById(id);
			if(user.getTwoInchPhotos() == null || user.getTwoInchPhotos().endsWith("default_headpic.png")){
				user.setTwoInchPhotos("view/images/default_headpic.png");
			}
			model.put("user", JsonUtil.Object2Json(user));
		} catch (Exception e) {
			e.printStackTrace();
		}
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/yg_info", model);
	}
	
	@RequestMapping("/yj")
	public ModelAndView yj(HttpServletRequest request, HttpServletResponse response, HttpSession session, Map<String,Object> model) {
		Config.init(request, response, session, model);
		return new ModelAndView("infoQry/yj", model);
	}
}
